dojo.declare("MShopCartPg", wm.Page, {
    start: function() {

    },
    "preferredDevice": "phone",

    button1Click: function(inSender) {

        //method for new check out
        //wm.Page.getPage("MCustomerHomePg").vPageBack = "CHECK_OUT";
        var accountId = app.vAccount.data.accountId;
        if (accountId) {

            this.largeTextArea1.clear();

            //hide the search
            var totalPrice = 0;
            this.totalOrderAmount = 0;
            for (var count = 0; count < app.vCartItems.getCount(); count++) {

                var vItem = app.vCartItems.getItem(count);
                // alert(vItem.data.tax);
                if (vItem.data.quantity > 0) {

                    var qty = vItem.data.quantity;
                    // alert('price' + vItem.data.itemPrice);
                    var price = (qty * vItem.data.itemPrice);
                    price = Math.round(price * 100) / 100;
                    totalPrice = totalPrice + price;
                    totalPrice = Math.round(totalPrice * 100) / 100;
                    //alert('totalPrice' + totalPrice);
                }
            }


            if (app.vCartItems.getCount() > 0) {
                this.totalOrderAmount = totalPrice;
                this.layerPlaceOrder.activate();
                this.panel2.setShowing(true);
                this.labelTotalOrderPrice.setShowing(true);
                // this.labelSelectItem.setShowing(false);
                this.labelTotalOrderPrice.setCaption("Total:" + totalPrice + " Rs");
                this.buttonConfirmOrder.setShowing(true);
                // this.labelReviewOrder.setShowing(true);
                // this.panel9.setShowing(false);
            } else {
                app.toastWarning("Please Select atleast one Item", 2000);

                // this.labelReviewOrder.setShowing(false);
                // this.labelSelectItem.setShowing(true);
                this.labelTotalOrderPrice.setShowing(false);
                this.buttonConfirmOrder.setShowing(false);


            }
        } else {
            app.toastWarning("Please login to checkout", 2000);
            wm.getPage("MHomePg").navButton2.click();
        }

    },
    buttonConfirmOrderClick: function(inSender) {
        if (this.totalOrderAmount > 0) {
            // this.svCompleteOrder.input.setValue("order.customerId", mainPage.vUserId);
            // this.svCompleteOrder.update();
            this.layerOrderResult.activate();
        } else {
            app.toastWarning("Please select atleast one item", 2000);
        }

    },
    svPlaceOrderSuccess1: function(inSender, inDeprecated) {

        app.vCartItems.clearData();

    },

    buttonBackClick: function(inSender) {
        if (wm.Page.getPage("MHomePg")) {
            wm.Page.getPage("MHomePg").navButton1.click();
        }
    },
    listShoppingCartItemNameFormat: function(inValue, rowId, cellId, cellField, cellObj, rowObj) {
        var itemsFormat = "<div class='MobileRow'>" + " " + rowObj.itemName + "</div>" + "<div class='MobileRow'>" + " " + rowObj.unit + "</div>" + "<div class='MobileRow'>" + "Selected Quantity: " + rowObj.quantity + "</div>" + "<div class='MobileRow'>" + "Item Price: " + rowObj.itemPrice + " " + rowObj.currency + "</div>";

        return itemsFormat;
    },
    listShoppingCartSelect: function(inSender, inItem) {
        var quantity = this.listShoppingCart.selectedItem.getData().quantity;
        if (quantity) {
            this.txtQuantity.setDisplayValue(quantity);
        }


        this.designableDialog1.show();
    },
    bAddQtyClick: function(inSender) {

        if (this.txtQuantity) {
            // alert(isNaN(this.txtQuantity.getDataValue()));
            if (this.txtQuantity.getDisplayValue()) {
                this.txtQuantity.setDisplayValue(+this.txtQuantity.getDataValue() + 1);
            } else {
                this.txtQuantity.setDisplayValue(1);
            }

        }


    },
    bRemoveQtyClick: function(inSender) {

        if (this.txtQuantity) {
            if (this.txtQuantity.getDisplayValue() > 0) {
                this.txtQuantity.setDisplayValue(+this.txtQuantity.getDisplayValue() - 1);
            } else {
                this.txtQuantity.setDisplayValue(0);
            }

        }
    },
    bAddToCartClick: function(inSender) {

        if (+this.txtQuantity.getDataValue() > 0) {

            var selItemId = this.listShoppingCart.selectedItem.getData().itemId;
            var homeMenu = wm.Page.getPage("MHomeMenuPg");

            for (var count = 0; count < homeMenu.lvItems.getCount(); count++) {

                var vItem = homeMenu.lvItems.getItem(count);

                if (vItem.data.itemId === selItemId) {


                    if (app.vCartItems.getCount() > 0) {
                        var isMatchFound = false;
                        var cartCount = app.vCartItems.getCount();
                        for (var count1 = 0; count1 < cartCount; count1++) {
                            var vcartItem = app.vCartItems.getItem(count1);
                            if (vcartItem.data.itemId === selItemId) {
                                vcartItem.data.quantity = +this.txtQuantity.getDataValue();
                                vItem.data.quantity = this.txtQuantity.getDataValue();
                                app.vCartItems.setItem(count1, vcartItem);
                                homeMenu.lvItems.setItem(count, vItem);
                                isMatchFound = true;


                            }
                        }
                        if (!isMatchFound) {
                            vItem.data.quantity = +this.txtQuantity.getDataValue();
                            homeMenu.lvItems.setItem(count, vItem);
                            app.vCartItems.addItem(vItem);

                        }

                    } else {
                        vItem.data.quantity = +this.txtQuantity.getDataValue();
                        homeMenu.lvItems.setItem(count, vItem);
                        app.vCartItems.addItem(vItem, count);

                        // var vItem1 = this.lvItems.getItem(count);
                        // alert(vItem1.data.quantity);
                    }
                }



            }
        }

        this.designableDialog1.hide();
    },
    _end: 0
});